<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product_Controller extends CI_Controller {
	
	function __construct()
    {
		parent::__construct();
		$this->load->model('product_model');
    }
	
	public function index()
	{
		$this->show_latest_products();
	}
	
	public function show_product_detail_http($folder, $year, $month, $product_id, $product_name='')
	{
		$this->show_product_detail(str_replace('.htm', '', $product_id));
	}
	
	public function show_product_detail_http_cache_404()
	{
		header('HTTP/1.1 200 OK');
		$uri_exploded = explode('_',$_SERVER['REQUEST_URI']);
		$uri_exploded = explode('/',$uri_exploded[2]);
		$this->show_product_detail(intval($uri_exploded[0]));
	}
	
	public function show_product_detail($product_id=0)
	{
		$this->load->model('comment_model');
		
		$product = $this->product_model->get_product_using_product_id($product_id);
		if($product->num_rows() == 1)
		{
			$product = $product->result();
			$product = $product[0];
			$product_last_modified_time = strtotime($product->post_modified);
			$cache_time_in_seconds = 0;
			http_not_modified($product_last_modified_time);
			set_http_cache_headers($product_last_modified_time, $cache_time_in_seconds);
			
			// get comments of product 
			$product_comments = $this->comment_model->get_approved_product_comments($product_id, 'comment_date');

			// get category
			$category = $this->product_model->get_category_using_id($product->category_id);
			$category = $category->result();
			$category = $category[0];
			
			// category banners 
			if(!empty($category->banner_1)) $this->banner_1 = $category->banner_1; 
			if(!empty($category->banner_2)) $this->banner_2 = $category->banner_2; 
			if(!empty($category->banner_3)) $this->banner_3 = $category->banner_3; 
			
			// html meta 
			$first_paragraph_end = strpos($product->product_desc, "</p>"); 
			$first_paragraph_end = $first_paragraph_end > 1000 ? 1000 : $first_paragraph_end;
			$first_paragraph_end = $first_paragraph_end == 0 ? 1000 : $first_paragraph_end; 

			$this->html_meta['page_title'] = strip_tags($product->product_title);
			$this->html_meta['meta_keywords'] = $product->meta_keywords;			
			$this->html_meta['meta_description'] = strip_tags(substr($product->product_desc, 0, $first_paragraph_end));
			$this->html_meta['robots'] = 'index, follow';
			$this->template->write_view('content', 'product_detail_block', array('product' => $product, 'category' => $category, 'product_comments' => $product_comments), FALSE);
			$this->template->write_view('content', 'related_block', array('related_products' => $this->product_model->get_related_product($product_id)), FALSE);
			$this->template->write_view('content', 'deals_block.php', NULL, FALSE);
			// write response content to file 
			$post_date = new DateTime($product->post_date);
			$this->http_cache_filepath = 'products'.date_format($post_date, '/Y/d' );
			$this->http_cache_filename = $product_id.'.htm';
			
			// write right view
			$this->template->write_view('sidebar_right', 'right_common_block', array('category'=>$category), FALSE);
		}
		else 
		{
			header('HTTP/1.1 404 Not Found');
			$this->template->write('content', '<div class="error">Product not found.</div>');
		}
		
		$this->template->write_view('header', 'header_detail_block', NULL, FALSE);
		
		$this->javascript_head = array
		(
			'/js/jquery-1.8.1.min.js'
			,'/js/jquery.tooltip.js'
		);
		$this->template->write_view('breadcrumbs', 'breadcrumbs_block', NULL, FALSE);
		$this->render_template = TRUE;	
	}
	
	public function show_latest_products()
	{
		$categories = $this->product_model->get_category_list(); 
		$products = $this->product_model->get_latest_products(10, 0, 10); 
		
		$this->template->write_view('content', 'product_list_block', array('products' => $products), FALSE);
		$this->template->write_view('content', 'category_list_block_static', NULL, FALSE);
		$this->render_template = TRUE;	
	}

	public function show_products_using_category_http($folder, $parent_n_sub_category_name='', $page_number=1)
	{
		$category = explode('_', $parent_n_sub_category_name);
		if(count($category) == 2)
		{
			$parent_category_name = $category[0];
			$category_name = $category[1];
		}
		else
		{
			$category_name = $parent_n_sub_category_name;
			$parent_category_name = '';
		}
		$this->show_products_using_category($category_name, str_replace('.htm', '', $page_number), $parent_category_name);
	}
	
	public function show_products_using_category_http_cache_404()
	{
		header('HTTP/1.1 200 OK');
		$uri_exploded = explode('/', str_replace($this->config->item('applicaton_path'), '', $_SERVER['REQUEST_URI']));
		if(count($uri_exploded) == 6)
		{
			$this->show_products_using_category($uri_exploded[2], $uri_exploded[4], '');
		}
		else 
		{
			$this->show_products_using_category($uri_exploded[3], $uri_exploded[5], $uri_exploded[2]);
		}
		
	}
	public function show_products_using_category($category_name='', $page_number=1, $parent_category_name='')
	{
		
		$page_number = intval($page_number);
		$page_number = $page_number < 1 ? 1 : $page_number;
		$limit = 9;
		$start = ($page_number - 1) * $limit;
		
		$category = $this->product_model->get_category_using_slug_parent_n_category_name($category_name, $parent_category_name);
			
		if($category->num_rows() == 1)
		{
			$category = $category->result();
			$category = $category[0];
			
			$category_post_last_modified_time = strtotime($category->post_modified);
			$cache_time_in_seconds = 60*60;
			http_not_modified($category_post_last_modified_time);
			set_http_cache_headers($category_post_last_modified_time, $cache_time_in_seconds);

			$category_product_count = $category->product_count;
			$products = $this->product_model->get_products_using_category_id($category->category_id, $start, $limit); 
			$subdomain_link = $this->product_model->get_subdomain_link_using_category_id($category->category_id);
			
			// html meta 
			$this->html_meta['page_title'] = '"'.$category->meta_title.'"';
			$this->html_meta['meta_keywords'] = '"'.$category->meta_keywords.'"';
			$this->html_meta['meta_description'] = '"'.$category->meta_desc.'"';
			$this->html_meta['robots'] = 'index, follow';
			$meta_vars['category_name'] = $category->category_name;
			$meta_vars['page_number'] = $page_number;
			process_html_meta($this->html_meta, $meta_vars);
			
			// banners 
			if(!empty($category->banner_1)) $this->banner_1 = $category->banner_1; 
			if(!empty($category->banner_2)) $this->banner_2 = $category->banner_2; 
			if(!empty($category->banner_3)) $this->banner_3 = $category->banner_3; 
			
		} 
		
		// $this->template->write_view('sidebar_left', 'category_list_block_static', NULL, FALSE);

		if(isset($products)) 
		{
			$this->load->library('pagination');
			
			$config['base_url'] = $this->config->config['base_url'].'/category'.(empty($parent_category_name) ? '' : "/$parent_category_name")."/$category_name/page/{page_number}/";
			$config['cur_page'] = $page_number;
			$config['total_rows'] = $category_product_count;
			$config['per_page'] = $limit;
			$config['rel'] = 'noindex,follow';
			$config['first_link'] = '&laquo;';
			$config['last_link'] = '&raquo;';
			$config['next_link'] = '&rsaquo;';
			$config['prev_link'] = '&lsaquo;';
			
			$this->pagination->initialize($config); 
			
			//template writing
			$this->template->write_view('content', 'product_list_block', array('block_title' => "Products By $category->category_name ", 'products' => $products, 'subdomain_link' => $subdomain_link, 'category' => $category, 'show_add' => TRUE, 'pagination' => $this->pagination->create_links()), FALSE);
			$this->template->write_view('content', 'deals_block.php', NULL, FALSE);
			// define canonical link
			$canonical_link = $this->config->config['base_url'].'/category'.(empty($parent_category_name) ? '' : "/$parent_category_name")."/$category_name/page/$page_number/";
			// $current_url = $this->config->config['site_url'].$_SERVER['REQUEST_URI'];
			// if($canonical_link != $current_url)
			$this->html_meta['canonical'] = $canonical_link;

			// write response content to file 
			$this->http_cache_filepath = 'categories'.(empty($parent_category_name) ? '/' : "/${parent_category_name}_")."$category_name";
			$this->http_cache_filename = $page_number.'.htm';
			
			// $this->template->write_view('sidebar_right', 'right_common_block', array('category' => $category), FALSE);

		}
		else 
		{
			header('HTTP/1.1 404 Not Found');
			$this->template->write('content', '<div class="error">Unknow Category.</div>');
		}
		
		$this->template->write_view('header', 'header_detail_block', NULL, FALSE);
		$this->render_template = TRUE;	
	}
	
	public function track()
	{
		// var_dump($_REQUEST); 
		// echo $_SERVER['PATH_INFO']; exit;
        $urlArray = parse_url($_SERVER['REQUEST_URI']);
        parse_str($urlArray['query'], $urlParameterArray);
		if( array_key_exists('url', $urlParameterArray))
		{
			$url = urldecode($urlParameterArray['url']);
			if(strrpos($url, 'http://') === false)
			$url = 'http://'.$url;
		}
		else
		{
			$url = $this->config->item('base_url');
		}
		
		header('Location: ' . $url, true, 301);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/product_controller.php */